/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/8/30
 * Time: 05:24
 */
$(function(){
    var menu = $.url().segment(1);
    if (location.href != config.domain) {
        if (!_token && !$.cookie()._token) {
            location.href = config.domain;
        }
        if (_token && $.cookie()._token != _token) {
            $.cookie('_token', _token, {expires: 1, path: '/'});
        }
        var authorization = $.cookie()._token;
        if (!authorization) {
            var url = config.domain;
            if (role == 1 && menu != 'teacher') {
                location.href = url + '/teacher/file.html';
            }
            if (role == 2 && menu != 'hro') {
                location.href = url + '/hro/teacher.html';
            }
            if (role == 3 && menu != 'finance') {
                location.href = url + '/finance/rent.html';
            }
            if (role == 4 && menu != 'asset') {
                location.href = url + '/asset/repair.html';
            }
            if (role == 5 && menu != 'manager') {
                location.href = url + '/manager/apart.html';
            }
        }
        sendAjax('/v1/base/info.json', 'get', {}, function(json){
            var user = json.body.data;
            $('.user-realname').text(user.realname);
            $('.user-role').text(user.role_name);
            $('.user-number').text('（'+user.number+'）');
            if (user.status != 1 && ($.url().attr('file') != 'file.html' && $.url().attr('file') != 'edit.html')) {
                location.href = '/teacher/file.html';
            }
        });
    }
});

function sendAjax(url, method, data, callbakck) {
    $.ajax({
        type: method,
        url: url,
        data: data,
        headers: {
            Authorization: $.cookie()._token,
            Accept: "application/json; charset=utf-8"
        },
        success: function(e) {
            var json = JSON.parse(e);
            callbakck(json);
        }
    });
}

function pay(url, method, data, callback) {
    $.ajax({
        type: method,
        url: url,
        async: true,
        data: data,
        headers: {
            Authorization: $.cookie()._token,
            Accept: "application/json; charset=utf-8"
        },
        success: function(e) {
            var json = JSON.parse(e);
            callback(json.body);
        },
        error: function (e) {
            var json = JSON.parse(e);
            alert(json.ret_content);
        }
    });
}